package com.jf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jf.entity.Classes;
import com.jf.entity.Student;
import com.jf.mapper.ClassesMapper;
import com.jf.mapper.StudentMapper;
import com.jf.service.IStudentService;
import com.jf.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements IStudentService {
	@Resource
	private StudentMapper studentMapper;
	@Resource
	private ClassesMapper classesMapper;
	@Override
	public List<Student> findStudents() {
		// 1.1 查询所有学生
		List<Student> students = studentMapper.selectAll();
		// 1.2 为学生添加班级名称
		for (Student student : students) {
			Integer cid = student.getCid();
			Classes classes = classesMapper.selectByPrimaryKey(cid);
			student.setCname(classes.getCname());
		}
		// 1.3 最后，返回处理后的学生列表
		return students;
	}

	/**
	 * 分页查询
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageResult<Student> findByPage(int page, int pageSize) {
		// 1. 开始分页
		PageHelper.startPage(page,pageSize);
		// 2. 查询所有数据
		List<Student> students = this.findStudents();
		// 3. 转换为Page对象
		Page<Student> studentPage = (Page<Student>) students;
		// 4. 最后，返回分页结果
		return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
	}
}
